package com.example.chatroom.api;


import com.example.chatroom.mapper.FriendMapper;
import com.example.chatroom.model.Friend;
import com.example.chatroom.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @RequestMapping("/friendList")
    @ResponseBody
    public List<Friend> getFriendList(HttpServletRequest req) {
        // 1. 先从会话中获取到 userId
        HttpSession session = req.getSession();
        if (session == null) {
            // 未登录
            System.out.println("session 不存在!");
            return new ArrayList<>();
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            System.out.println("无好友!");
            return new ArrayList<>();
        }
        // 2. 根据数据库查数据
        return friendMapper.selectFriendList(user.getUserId());
    }
}
